<?php

namespace app\admin\controller;

use app\models\CompanyModel;

/**
 * 集团管理
 * User: Small Y
 * Date: 2017/03/13
 * @package app\admin\controller
 */
class Company extends Common
{
	/**
	 * @var CompanyModel
	 */
    protected $companyModel;

    /**
     * 集团管理.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */
    public function lists()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $regional = $this->companyModel->getCompanyLists($param);
            $this->success('success', '', $regional, 0);
        }

        return $this->fetch();
    }

    /**
     * 添加集团.
     *
     * @return mixed
     */
    public function company_add()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if (false !== $this->companyModel->save($data)) {
                $this->success('添加成功', url('Company/lists'), '', 1);
            }
            $this->error('添加失败', null, '', 1);
        }

        return $this->fetch();
    }

    /**
     * 编辑集团.
     *
     * @return mixed
     */
    public function company_edit()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if (!$this->companyModel->save($data, ['id' => $data['id']])) {
                $this->error('编辑失败', null, '', 1);
            }
            $this->success('编辑成功', url('Company/lists'), '', 1);
        }
    }

    /**
     * 更改集团状态.
     *
     * @return mixed
     */
    public function company_status()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if($data['status'] == 1){
                $msg = '启用';
            }else{
                $msg = '禁用';
            }
            if (!$this->companyModel->save($data, ['id' => $data['id']])) {
                $this->error($msg.'失败', null, '', 1);
            }
            $this->success($msg.'成功', '', '', 1);
        }
    }

	/**
	 * 实例化.
	 * modify by Small Y
	 *
	 * @return bool|void
	 */
    protected function _initialize()
    {
        parent::_initialize();
        $this->companyModel = new CompanyModel();
    }
}
